<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\staff;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class IndexController extends CommonController
{
	//lxylxylxylxylxylxylxylxylxylxylxylxylxylxylxylxylxylxylxylxylxylxylxy
    //后台主页
    public function index()
    {
        //测试连接
//        echo 'D:\Laravel5\zuche\app\Http\Controllers\Admin\IndexController.php';
//        //测试数据库连接
//        $pdo=DB::connection()->getPdo();
//        dd($pdo);//dd()与print_r + die;一个意思。

        //载入模板
        return view('admin.index');

    }

    //后台欢迎页面
    public function welcome()
    {
        //载入模板
        return view('admin.welcome');

    }
	
	//wcxwcxwcxwcxwcxwcxwcxwcxwcxwcxwcxwcxwcxwcxwcxwcxwcx
    //修改密码
    public function password()
    {
//        Input::
        if($input = Input::all()){
            $rules = [
                'newpassword' =>'required|between:6,20|confirmed',
            ];
            $message = [
                'newpassword.required' =>"请输入新密码！",
                'newpassword.between' =>"请输入6-20位新密码！",
                'newpassword.confirmation' =>"新密码和确认密码不一致！",
            ];
//            Validator::
            $validator = Validator::make($input,$rules,$message);
            if($validator->passes()){
//                staff::
                $oldP = staff::where("sid",session("staff_id"))->value("password");
//                Crypt::
                $oldP = Crypt::decrypt($oldP);//密码转义
                if($oldP != $input['password']){//判断输入的原密码和数据库是否一致
                    return back()->with("errors","原密码错误！");
                }else{
                    $password = Crypt::encrypt($input['newpassword']);
                    $re =  staff::where("sid",session("staff_id"))->update(["password"=>$password]);//修改
                    if($re){
                        session(["staff_id"=>null]);
                        return redirect('admin/');
                    }else{
                        return back()->with("errors","修改失败，请稍候再试！");
                    }
                }
            }else{
                return back()->withErrors($validator);
            }
        }
        return view("admin.index.editpassword");
    }


}
